package com.example.sunlight.interneteverything.View;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.sunlight.interneteverything.R;


/**
 * Created by SunLight on 2017/8/17.
 */

public class TraffcView extends View {
    TypedArray typedArray;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    TraffcView.this.invalidate();
                    break;
            }
        }
    };
    SizeChange sizeChange;

    public TraffcView(Context context) {
        super(context);
        init();
    }

    public void init() {
        sizeChange = new SizeChange();
        sizeChange.start();
    }

    String color;

    public void init(AttributeSet attrs) {
        typedArray=getContext().obtainStyledAttributes(attrs, R.styleable.TraffcView);
        color = typedArray.getString(R.styleable.TraffcView_color_t);
        if (color.equals("red")) {
            kit1 = Color.argb(200, 255, 0, 12);
            kit = Color.argb(100, 255, 0, 12);
        } else {
            kit1 = Color.argb(200, 240, 93, 49);
            kit = Color.argb(100, 240, 93, 49);
        }
    }

    public TraffcView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        init(attrs);
    }

    public TraffcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        init(attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(30, 30);
    }

    int kit1, kit;
    int size;
    boolean isState = true;

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate((getWidth() + getPaddingLeft() - getPaddingRight()) / 2,
                (getHeight() + getPaddingTop() - getPaddingBottom()) / 2);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(20);
        paint.setColor(kit1);
        canvas.drawCircle(0, 0, 5, paint);

        paint.setColor(kit);
        canvas.drawCircle(0, 0, 5 + size, paint);
    }

    class SizeChange extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(70);
                    if (isState) {
                        if (size <= 10) {
                            size++;
                        } else {
                            isState = false;
                        }
                    } else {
                        if (size >= 0) {
                            size--;
                        } else {
                            isState = true;
                        }
                    }


                    handler.sendEmptyMessage(0x123);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
    }
}
